package com.ctms.system.controller;

import com.ctms.common.utils.*;
import com.ctms.system.domain.*;
import com.ctms.system.service.StudentService;
import com.ctms.system.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping("/student")
@Controller
public class StudentController {
    @Autowired
    StudentService studentService;
    @Autowired
    TeacherService teacherService;

    @GetMapping("")
    public String student(Model model){
        UserDO userDO = ShiroUtils.getUser();
        int identity = userDO.getIdentity();
        String name = userDO.getName();
        //判断登录人是否是班长，默认为0，不是班长
        int monitorFlag=0;
        //所有班长
        List<String> monitor = studentService.selectAllMonitor();
        for (String str : monitor){
            if (str.equals(name)){
                monitorFlag=1;
            }
        }
        model.addAttribute("identity",identity);
        model.addAttribute("monitorFlag",monitorFlag);
        return "sys/student/student";
    }

    @GetMapping("/list")
    @ResponseBody
    PageUtils list(@RequestParam Map<String,Object> params){
        Query query = new Query(params);
        List<UserDO> userDOS = studentService.selectAllStudent(params);
        int total = studentService.count(query);
        PageUtils pageUtils = new PageUtils(userDOS,total);
        return pageUtils;
    }

    @GetMapping("/add")
    public String add(Model model){
        return "sys/student/add";
    }
    @PostMapping("/save")
    @ResponseBody
    R insert(UserDO userDO){
        //获取时间
        DateUtils dateUtils = new DateUtils();
        String time = dateUtils.getDate();
        //获取班级id
        int class_id = Integer.parseInt(userDO.getStudentClass());

        userDO.setGmtCreate(time);
        userDO.setGmtModified(time);
        userDO.setIdentity(2);
        userDO.setStatus(1);
        userDO.setPassword("123456");
        //将班级id转化为name
        String student_class = userDO.getStudentClass();
        String name = studentService.getClassNameById(Integer.parseInt(student_class));
        userDO.setStudentClass(name);
        if (studentService.insertStudent(userDO)>0){
            //班级学生中间表
            ClassStudentDO classStudentDO = new ClassStudentDO();
            int user_id = studentService.selectMaxIdByTime();
            classStudentDO.setClassId(class_id);
            classStudentDO.setUserId(user_id);
            studentService.insertClassStudent(classStudentDO);
            //用户角色中间表
            int id = studentService.selectMaxIdByTime();
            int role_id = 3;
            UserRoleDO userRoleDO=new UserRoleDO();
            userRoleDO.setUserId(Long.parseLong(Integer.toString(id)));
            userRoleDO.setRoleId(Long.parseLong(Integer.toString(role_id)));
            teacherService.insertUserRole(userRoleDO);
        }
        return R.ok();
    }

    @GetMapping("/edit/{userId}")
    public String edit(Model model,@PathVariable("userId") Integer userId){
        UserDO userDO = studentService.selectById(userId);
        Integer id = studentService.getClassIdByName(userDO.getStudentClass());
        userDO.setStudentClass(String.valueOf(id));
        model.addAttribute("student",userDO);
        return "sys/student/edit";
    }

    @PostMapping("/update")
    @ResponseBody
    R update(UserDO userDO){
        int user_id = Integer.parseInt(Long.toString(userDO.getUserId()));
        int class_id = Integer.parseInt(userDO.getStudentClass());
        String name = studentService.getClassNameById(class_id);
        userDO.setStudentClass(name);
        if (studentService.updateStudent(userDO)>0){
            ClassStudentDO classStudentDO = new ClassStudentDO();
            classStudentDO.setUserId(user_id);
            classStudentDO.setClassId(class_id);
            studentService.updateClassStudent(classStudentDO);
            return R.ok();
        }else {
            return R.error();
        }
    }

    @PostMapping("/remove")
    @ResponseBody
    R delete(Integer userId){
        if (studentService.deleteStudent(userId)>0){
            //删除班级学生中间表
            studentService.deleteClassStudent(userId);
            //删除用户角色中间表
            teacherService.deleteUserRole(userId);
            return R.ok();
        }else {
            return R.error();
        }
    }

    @PostMapping("/getClass")
    @ResponseBody
    public List<ClassDO> get(){
        List<ClassDO> list = studentService.getAllClass();
        return list;
    }

    @PostMapping("/getCourse")
    @ResponseBody
    public List<CourseDO> getCourse(){
        List<CourseDO> list = studentService.getAllCourse();
        return list;
    }
}
